package com.arthur.system.controller;

import java.util.List;

import javax.servlet.ServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tk.mybatis.mapper.entity.Example;

import com.arthur.common.controller.BaseController;
import com.arthur.common.util.DynamicSpecifications;
import com.arthur.shopping.domain.Goods;
import com.arthur.shopping.domain.Type;
import com.arthur.shopping.service.GoodsService;
import com.arthur.shopping.service.TypeService;

@Controller
public class IndexController extends BaseController {
	
	@Autowired
	private TypeService typeService;
	
	@Autowired
	private GoodsService goodsService;

	@RequestMapping("/")
	public String index(Long typeid, Model model) {
		try {
			if(typeid != null && typeid !=0){
				Type type = typeService.findById(typeid);
				model.addAttribute("typeName", type.getTypeName());
			}
		} catch (Exception e) {
		}
		return "index/index";
	}
	
	@RequestMapping("/lr/generic/{gid}")
	public String generic(@PathVariable Long gid,Model model) {
		model.addAttribute("gid", gid);
		return "index/generic";
	}
	
	@RequestMapping("lr/type/list")
	@ResponseBody
	public List<Type> typeList(Type type) {
		try {
			return this.typeService.findAllTypes(type);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	@RequestMapping("lr/goods/list")
	@ResponseBody
	public List<Goods> goodsList(ServletRequest request, Goods goods) {
		Example example = DynamicSpecifications.bySearchFilter(request,Goods.class,goods);
		List<Goods> list = this.goodsService.selectByExample(example);
		return list;
	}
	
	@RequestMapping("lr/goods/info/{gid}")
	@ResponseBody
	public Goods getGoods(@PathVariable Long gid){
		Goods goods = goodsService.selectByKey(gid);
		return goods;
	}
	
}
